package com.zmy.xcx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmy.xcx.dao.entity.COPDMedicineDO;
import com.zmy.xcx.dao.mapper.COPDMedicineMapper;
import com.zmy.xcx.dto.req.COPDMedicineReqDTO;
import com.zmy.xcx.dto.resp.COPDMedicineRespDTO;
import com.zmy.xcx.service.COPDMedicineService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * @Author Melancholy
 * @Date 2025/4/13
 * @Description: COPDҩƷ�߼�ʵ�ֲ�
 */
@Service
public class COPDMedicineServiceImpl extends ServiceImpl<COPDMedicineMapper, COPDMedicineDO> implements COPDMedicineService {

    /**
     * �ݴ�����
     */
    private final static ConcurrentHashMap<Integer, Integer> clickCounter = new ConcurrentHashMap<>(128);

    /**
     * IO�ܼ���,������� & С,���ѡ������̳߳�
     */
    private static final ExecutorService persistClicksPool = Executors.newCachedThreadPool();


    @Override
    public List<COPDMedicineRespDTO> listCOPDMedicineInfo(COPDMedicineReqDTO copdMedicineReqDTO) {
        return new LambdaQueryChainWrapper<>(COPDMedicineDO.class)
                .like(StrUtil.isNotBlank(copdMedicineReqDTO.getMedicineName()), COPDMedicineDO::getMedicineName, copdMedicineReqDTO.getMedicineName())
                .like(StrUtil.isNotBlank(copdMedicineReqDTO.getCategorySymptom()), COPDMedicineDO::getCategorySymptom, copdMedicineReqDTO.getCategorySymptom())
                .eq(StrUtil.isNotBlank(copdMedicineReqDTO.getInhalationType()), COPDMedicineDO::getInhalationType, copdMedicineReqDTO.getInhalationType())
                .eq(ObjectUtil.isNotNull(copdMedicineReqDTO.getIsNebulizer()), COPDMedicineDO::getIsNebulizer, copdMedicineReqDTO.getIsNebulizer())
                .eq(ObjectUtil.isNotNull(copdMedicineReqDTO.getIsPrescription()), COPDMedicineDO::getIsPrescription, copdMedicineReqDTO.getIsPrescription())
                .list()
                .stream()
                .map((copdMedicineDO) -> BeanUtil.toBean(copdMedicineDO, COPDMedicineRespDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    public void incrementClick(Integer id) {
        clickCounter.merge(id, 1, Integer::sum);
    }

    /**
     * ���� clickCounter �е�����,�ɶ�ʱ����־û������ݿ�
     * �ڶ�ʱ���񴥷�ʱ,��ʱ�ĵ�����ᶪʧ.��Ҫѡ�񲻶�ʧ,���Բ��ü����ķ�ʽ
     */
    public void batchPersistClicks() {
        clickCounter.forEach((key, value) -> persistClicksPool.execute(() -> {
            new LambdaUpdateChainWrapper<>(COPDMedicineDO.class)
                    .eq(COPDMedicineDO::getId, key)
                    .setSql("click_count = click_count + " + value)
                    .update();
        }));

        // ��յ�ǰ��ϣ������
        clickCounter.clear();
    }
}
